package com.schoolhelper.controller.activity;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hyphenate.easeui.EaseIM;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.provider.EaseUserProfileProvider;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.schoolhelper.controller.fragment.ConversationListFragment;
import com.schoolhelper.model.Model;
import com.schoolhelper.model.dao.okhttp.LwlRequestUrl;
import com.schoolhelper.model.entity.User;
import com.schoolhelper.utils.constant.LwlApi;

import java.util.HashMap;
import java.util.Map;

public class ConversationListActivity extends FragmentActivity {

    private ConversationListFragment conversationListFragment;
    private BottomNavigationView mainNavigationView;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = item -> {
        Intent intent;
        switch (item.getItemId()) {
            case R.id.main_navigation_main:
                intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(0, 0);
                return true;
            case R.id.main_navigation_message:
                return false;
            case R.id.main_navigation_person:
                intent = new Intent(getApplicationContext(), AccountActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(0, 0);
                return true;
            default:
                Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
                return false;
        }
    };
    private EaseTitleBar conversationTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation_list);

        initData();

        setFragment(conversationListFragment);

        initListener();
    }

    private void initListener() {
        mainNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        conversationTitleBar.setOnRightClickListener(v -> {
            Intent toSearchContact = new Intent(getApplicationContext(), SearchContactActivity.class);
            startActivity(toSearchContact);
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
//        conversationListFragment.conversationListLayout.refreshList();
        conversationListFragment.onRefresh();
    }

    private void setFragment(ConversationListFragment conversationListFragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.fl_conversation_list, conversationListFragment).commit();
    }

    private void initData() {
        conversationListFragment = new ConversationListFragment();
        mainNavigationView = findViewById(R.id.main_bottom_view);
        mainNavigationView.setSelectedItemId(R.id.main_navigation_message);

        conversationTitleBar = findViewById(R.id.conversation_title_bar);
        conversationTitleBar.setRightImageResource(R.drawable.ic_baseline_add_30);

        EaseIM.getInstance().setUserProvider(new EaseUserProfileProvider() {
            @Override
            public EaseUser getUser(String username) {
                Map<String, String> headers = new HashMap<>();
                Map<String, String> requestBody = new HashMap<>();
                requestBody.put("phone", username);
                String postResponseJson = LwlRequestUrl.getPostResponseJson(LwlApi.BASE_URL, LwlApi.GET_BY_PHONE, headers, requestBody);
                System.out.println("查询结果：" + postResponseJson);
                try {
                    User user = JSONObject.parseObject(postResponseJson, User.class);
                    EaseUser easeUser = new EaseUser(username);
                    easeUser.setNickname(user.getUsername());
                    easeUser.setAvatar(user.getProfileImg());
                    return easeUser;
                } catch (Exception e) {
                    runOnUiThread(() -> {
                        Toast.makeText(getApplicationContext(), "查询失败", Toast.LENGTH_SHORT).show();
                    });
                    e.printStackTrace();
                    return null;
                }
            }
        });
    }
}